package com.seeyon.apps.ext.cop.button.util;

import snail.common.utils.JSONHelper;
import snail.common.utils.MapHelper;
import snail.common.utils.db.DBHelper;
import snail.common.utils.db.DataSourceHelper;
import snail.common.web.HTTPHelper;
import snail.common.web.HTTPRequestHelper;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TestBenDi {
    //正式数据源
    private static String dburl = "jdbc:mysql://10.211.55.2:3306/cop?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8";// 连接URL字符串
    private static String user = "root";// 数据库用户名
    private static String password = "Mysql123456";// 用户密码
    private static String url = "http://10.37.129.2:8989/selectDept";
    @SuppressWarnings("unchecked")
    public static String getToken() {

        String url = "http://127.0.0.1:8989/login";

        String body = MapHelper.builder("name", "李维康")
                .put("password", "123456")
                .toJson();
        Map<String, Object> header = MapHelper.builder("Content-Type", "application/json").body();

        Map<String, Object> result = (Map<String, Object>) JSONHelper.parse(HTTPHelper.post(url, body, header));

        return MapHelper.getString(result, "msg");
    }
    public static List<Map<String, Object>> selectList(String id) {

        // 登录认证逻辑，用户名和密码正确时返回true即可
        DataSource ds = DataSourceHelper.getMySQL6XDatasource(dburl, user, password);
        DBHelper dbhelper = DBHelper.getInstance(ds);
        List<Map<String, Object>> userList = new ArrayList<>();
        try {
            userList = dbhelper.queryList("select a.name,a.PATH,a.ID,b.OBJECTIVE1_ID,d.name username,c.name postName from ORG_UNIT a left join ORG_RELATIONSHIP b on \n" +
                    "a.id = b.OBJECTIVE0_ID left join org_role c on b.OBJECTIVE1_ID = c.id left join ORG_MEMBER d\n" +
                    "on b.source_id = d.id where (c.code = 'DepLeader' or c.code = 'DepManager' or c.code = 'DepAssistant') and a.id = ?", id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return userList;
    }
    public static String selectSuperiorName(Long id) {

        // 登录认证逻辑，用户名和密码正确时返回true即可
        DataSource ds = DataSourceHelper.getMySQL6XDatasource(dburl, user, password);
        DBHelper dbhelper = DBHelper.getInstance(ds);
        try {
            return dbhelper.queryString("select name from org_unit where id = ?", id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }



    public static void main(String[] args) {
        String a = "a";
        String b = "b";
        a.equals(b);

        System.out.println(selectSuperiorName(-4967290939782183514l));
        List list = new ArrayList();
        for (Map<String, Object> userOas : selectList("-4967290939782183514")) {
            if (userOas.get("postname").equals("DepManager")) {
                list.add(userOas.get("username"));
            }
        }
        System.out.println(list);
        //System.out.println(selectList("-4967290939782183514l"));

        String body = MapHelper.builder("deptId", "123")
                .put("deptName","琦琦")
                .put("superiorName", "琦琦")
                .put("depManagers", list)
                .put("depLeader", list)
                .put("assistant", list)
                .toJson();
        try {
            System.out.println(body);

            Map<String, Object> header = MapHelper.builder("Content-Type", "application/json").body();

            String ab = HTTPHelper.post(url, body, header);

            Map<String, Object> result = (Map<String, Object>) JSONHelper.parse(HTTPHelper.post(url, body, header));

            List lists = new ArrayList<>();

            lists.add(ab);
            System.out.println(result.get("status"));

            System.out.println(lists);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.print("resultString");
    }
}
